<?php 
  //  include_once("cfg.php"); 
    include_once("adagps.com/excel/class/Writer.php");
    define('SEP_ROW',';xrowx;');
    define('SEP_COL',';xcolx;');
    define('SEP_TITLE',';xtitlex;');
    $export_type = $_POST['export_type'];
    if($export_type=='export_excel'){
    //echo '<pre>'; print_r($_POST); die();        
        
    $workbook = new Spreadsheet_Excel_Writer(); 
    $workbook ->setTempDir(getcwd());
     //   $workbook = new Spreadsheet_Excel_Writer();  
    $workbook->setVersion(8, 'utf-8');
    $format_bold_title =& $workbook->addFormat(array('Bold' => true,'Align' => 'center'));
    $format_bold_condition =& $workbook->addFormat(array('Bold' => true,'Align' => 'left'));
    $format_bold_condition_title =& $workbook->addFormat(array('Bold' => true,'Align' => 'right'));
    $format_bold =& $workbook->addFormat(array('Bold' => true,'Align' => 'left'));
    $format_tbl_header =& $workbook->addFormat(array('Bold' => true, 'Align' => 'center', 'BgColor' => 'gray', 'Border' => '1px'));
    $format_border =& $workbook->addFormat(array('Border' => '1px'));
    $format_border_center =& $workbook->addFormat(array('Border' => '1px', 'Align' => 'center'));
    $format_search_label =& $workbook->addFormat(array('Bold' => true, 'Align' => 'right'));
    $format_group =& $workbook->addFormat(array('Bold' => true, 'Border' => '1px'));
    $format_border_right =& $workbook->addFormat(array('Border' => '1px', 'Align' => 'right'));
    $format_group_footer =& $workbook->addFormat(array('Bold' => true, 'Border' => '1px')); 
    $cell =& $workbook->addFormat();
    $cell->setTextWrap();

    $worksheet =& $workbook->addWorksheet();
    $worksheet->setInputEncoding('utf-8');
 
  $strtitle =  $_POST['reporttitle'];   
  $strCondition =  $_POST['reportcondition'];
//echo '<pre>'; print_r($_POST);die();
  $strheader = $_POST['header'];
  $strbody = $_POST['body'];
  $strfooter = $_POST['footer'];   

    // Row Index
    $iRowIndex = 0;

    // 
  $arrHeader = explode(SEP_ROW,$strheader); 
  $header0 = $arrHeader[0] ;
  $numcol = 0;
  $arrcol = explode(SEP_COL,$strheader);   
  for($i=0;$i<count($arrcol);$i++){
      $arrdetail = explode(SEP_TITLE,$arrcol[$i]);
      $numcol += (int) $arrdetail[1];
  }
   
    //start title cá»§a file excel
    $worksheet->setMerge($iRowIndex, 1, 0, $numcol);
    $worksheet->write($iRowIndex, 1, $strtitle, $format_bold_title);
    $iRowIndex++;
    //end viáº¿t title cho  file Excel
    
    //start Condition cá»§a file excel
   
   // $arrCondition = explode(SEP_ROW,$strCondition);
//  for($i=0;$i<count($arrCondition);$i++){
//        $arrtitle = explode(SEP_TITLE,$arrCondition[$i]);
//        $temptitle =  $arrtitle[0];
//        $tempvalue =  $arrtitle[1];
//        $indexCellTitle =  (int) ($numcol/2);
//        $worksheet->setMerge($iRowIndex, 1, $iRowIndex, $indexCellTitle);
//        $worksheet->write($iRowIndex,1, $temptitle." :", $format_bold_condition_title);
//        $worksheet->setMerge($iRowIndex, $indexCellTitle+1, $iRowIndex, $numcol);
//        $worksheet->write($iRowIndex,$indexCellTitle+1, $tempvalue, $format_bold_condition); 
//        $iRowIndex++;   
//  }

  //end viáº¿t Condition cho  file Excel 
  if(strlen($strheader)>0){  
       for($i=0;$i<count($arrHeader);$i++){

           $headerDetail = $arrHeader[$i];
           if($headerDetail!='') {               
               $cellArr =   explode(';xcolx;',$headerDetail); 
               $colIndex = 1; 
               for($ce=0;$ce<count($cellArr);$ce++){
                    $cellDetail =  explode(SEP_TITLE,$cellArr[$ce]);
                    $colspan = (int)$cellDetail[1];
                    if($colspan>$numcol){
                       $colspan =   $numcol;
                    }
                    $value = $cellDetail[0];
                    if($colspan>1){
                     $worksheet->setMerge($iRowIndex, $colIndex, $iRowIndex, $colIndex + $colspan -1);
                     $worksheet->write($iRowIndex,$colIndex, $value, $format_tbl_header);
                     for($k=1;$k<$colspan;$k++){
                       $worksheet->write($iRowIndex,$colIndex+$k, '', $format_tbl_header);    
                     }
                     $colIndex+=$colspan;   
                    }else{
                     $worksheet->write($iRowIndex,$colIndex, $value, $format_tbl_header);
                     $colIndex++;  
                    }    
               }          
               $iRowIndex++;  
           }
           }   
       }
    // end ghi header 
         
        //start Ghi body
                if(strlen($strbody)>0){  
    $arrBody = explode(SEP_ROW,$strbody);  
       for($i=0;$i<count($arrBody);$i++){

           $cellArr =   explode(SEP_COL,$arrBody[$i]);
           $colIndex = 1; 
           for($ce = 0;$ce<count($cellArr);$ce++){
                $cellDetail =  explode(SEP_TITLE,$cellArr[$ce]);
                $colspan = (int)$cellDetail[1];
                $value = $cellDetail[0];
                if($colspan>$numcol){
                       $colspan =   $numcol;
                }
                if($colspan>1){
                 $worksheet->setMerge($iRowIndex, $colIndex, $iRowIndex, $colIndex + $colspan-1);
                 $worksheet->write($iRowIndex,$colIndex, $value, $format_border);
                 for($k=1;$k<$colspan;$k++){
                       $worksheet->write($iRowIndex,$colIndex+$k, '', $format_border);    
                 }
                 $colIndex+=$colspan;   
                }else{
                 $worksheet->write($iRowIndex,$colIndex, $value, $format_border);
                 $colIndex++;  
                }    
           }
           
           $iRowIndex++;   
        } 
       }
    // end ghi body
    
            //start Ghi body
    if(strlen($strfooter)>0){   
    $arrFooter = explode(SEP_ROW,$strfooter);  

       for($i=0;$i<count($arrFooter);$i++){

               
           $cellArr =   explode(SEP_COL,$arrFooter[$i]);
           $colIndex = 1; 
           for($ce = 0;$ce<count($cellArr);$ce++){
                $cellDetail =  explode(SEP_TITLE,$cellArr[$ce]);
                $colspan = (int)$cellDetail[1];
                $value = $cellDetail[0];
                if($colspan>$numcol){
                       $colspan =   $numcol;
                }
                if($colspan>1){
                 $worksheet->setMerge($iRowIndex, $colIndex, $iRowIndex, $colIndex + $colspan-1);
                 $worksheet->write($iRowIndex,$colIndex, $value, $format_border);
                 $colIndex+=$colspan; 
                  for($k=1;$k<$colspan;$k++){
                       $worksheet->write($iRowIndex,$colIndex+$k, '', $format_border);    
                 }  
                }else{
                 $worksheet->write($iRowIndex,$colIndex, $value, $format_border);
                 $colIndex++;  
                }    
           }           
           $iRowIndex++;    
               
           }
       }
    // end ghi body 
   
    $sCurrDate = date('His');
    $strtitle =vn_str_filter($strtitle) ; 
    $sFile = str_replace(' ','',$strtitle).'_'.$sCurrDate.'.xls';
    $workbook->send($sFile);
    $workbook->close();
  
        
    }else if($export_type=='export_pdf'){
    
    define('ROOT_PATH', './');
    define('CORE_PATH', ROOT_PATH.'core/');     
    define('PDF_LOGO','logo_example.gif');
    define('PDF_LINKS', 'cafe.wew.vn');
    define ('PDF_HEADER_LOGO_WIDTH', 30);
           
    require_once(CORE_PATH.'classes/tcpdf/config/lang/eng.php');
    require_once(CORE_PATH.'classes/tcpdf/tcpdf.php');

    // create new PDF document
    $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8');

    // set document information
    $pdf->SetCreator('Maginwan.com');
    $pdf->SetAuthor('Magiwan.com');
    $pdf->SetTitle('abc');
    $pdf->SetSubject('REPORT_HISTORY_STATUS_TITLE_EXCEL');
    $pdf->SetKeywords('REPORT_HISTORY_STATUS_TITLE_EXCEL');
    $pdf_title = $_POST['export_title'];

    // set default header data
    $pdf->SetHeaderData(PDF_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_LINKS, '');

    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    //set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

    //set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    //set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

    //set some language-dependent strings
    $pdf->setLanguageArray($l);

    // ---------------------------------------------------------

    // set font
    $pdf->SetFont('dejavusans', '', 8);

    $pdf->SetPageOrientation('L', true);

    $pdf->AddPage();   
    $html =  str_replace('\"', '"',$_POST['pdfcontent']);   
    $pdf->WriteHTML(($html), true, false, false, false, '');
    $pdf_title =vn_str_filter($pdf_title) ;
    //echo $pdf_title;
    $pdf->Output($pdf_title.date('His').'.pdf', 'D');        
    //print $html;
    }
    
    function vn_str_filter ($str){
        $unicode = array(
            'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
            'd'=>'đ',
            'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
            'i'=>'í|ì|ỉ|ĩ|ị',
            'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
            'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
            'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
            'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
            'D'=>'Đ',
            'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
            'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
            'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
            'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
            'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
        );
        
       foreach($unicode as $nonUnicode=>$uni){
            $str = preg_replace("/($uni)/i", $nonUnicode, $str);
       }
        return $str;
    }
     
    
?>
